<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="end" style="margin-bottom: 20px;">
          <el-button type="primary" size="small" @click="showDialog({ type: 1, pid: '0' })">添加权限</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table
          :data="list"
          style="width: 100%"
          border
          row-key="id">
          <el-table-column
            prop="name"
            label="名称">
          </el-table-column>
          <el-table-column
            prop="code"
            label="标识">
          </el-table-column>
          <el-table-column
            prop="description"
            label="描述">
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="showDialog({ type: 2, pid: row.id })">添加</el-button>
              <el-button type="text" @click="showEditDialog(row.id)">编辑</el-button>
              <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <!-- <permission-dioalog 
      :dialogVisible="dialogVisible"
      @close-dialog="closeDialog">
    </permission-dioalog> -->
    <!-- <permission-dioalog 
      :value="dialogVisible"
      @input="dialogVisible = $event">
    </permission-dioalog> -->
    <permission-dioalog
      ref="permissionDialogRef"
      v-model="dialogVisible"
      :type="type"
      :pid="pid"
      :permissionForm="permissionForm"
      :inputValue="inputValue"
      @change-permission-form="changePermissionForm"
      @changeInputValue="changeInputValue"
      @get-permission-list="getPermissionList">
    </permission-dioalog>
  </div>
</template>

<script>
import { getPermissionList, deletePermission, getPermissionDetail } from '@/api/permission'
import { tranTree } from '@/utils/transTree'
import PermissionDioalog from './components/permission-dialog.vue'

export default {
  components: {
    PermissionDioalog
  },
  data() {
    return {
      list: [],
      dialogVisible: false,
      type: 0,
      pid: '',
      permissionForm: {
        name: '', // 权限名称
        code: '', // 权限标识
        description: '', // 权限描述
        enVisible: '0' // 权限启用标识
      },
      inputValue: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限点列表数据
    async getPermissionList() {
      const res = await getPermissionList()
      // console.log(res);
      this.list = tranTree(res.data)
    },
    // 点击添加按钮时，打开弹窗
    showDialog({ type, pid }) {
      this.type = type
      this.pid = pid
      this.dialogVisible = true
    },
    // 关闭弹窗
    // closeDialog() {
    //   this.dialogVisible = false
    // }
    // 点击编辑按钮时，打开弹窗
    async showEditDialog(id) {
      this.dialogVisible = true
      // this.$refs.permissionDialogRef.getPermissionDetail(id)
      const res = await getPermissionDetail(id)
      this.permissionForm = res.data
      this.inputValue = '123'
    },
    // 删除
    deletePermission(id) {
      // 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 发起删除请求
        await deletePermission(id)
        // 提示用户
        this.$message.success('删除权限点成功')
        // 刷新表格数据
        this.getPermissionList()
      })
    },
    // 修改表单数据对象
    changePermissionForm(form) {
      this.permissionForm = form
    },
    changeInputValue(value) {
      this.inputValue = value
    }
  }
}
</script>

<style>

</style>